<?php

namespace HelloWeb\UserBundle\Entity;

use Library\BaseEntity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;

/**
 * HelloWeb\UserBundle\Entity\User
 *
 * @ORM\Table(name="hw_users")
 * @ORM\Entity(repositoryClass="HelloWeb\UserBundle\Entity\Repository\UserRepository")
 */
class User extends BaseEntity
{
    /**
     * @var integer $id
     *
     * @ORM\Column(name="id", type="bigint")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;
    
    /**
     * @var string $firstName
     *
     * @ORM\Column(name="first_name", type="string", length=255, nullable=false)
     */
    private $firstName;
    
    /**
     * @var string $lastName
     *
     * @ORM\Column(name="last_name", type="string", length=255, nullable=false)
     */
    private $lastName;
    
    /**
     * @var string $position, e.g manager, director
     *
     * @ORM\Column(name="position", type="string", length=255, nullable=true)
     */
    private $position;

    /**
     * @var string $email
     *
     * @ORM\Column(name="email", type="string", length=255, nullable=true)
     */
    private $email;
    
    /**
     * @var string $phone
     *
     * @ORM\Column(name="phone", type="string", length=20, nullable=true)
     */
    private $phone; 
   
    /**
     * @var string $mobilePhone
     *
     * @ORM\Column(name="mobile", type="string", length=20, nullable=true)
     */
    private $mobilePhone;
 
    /**
     * @var ArrayCollection $roles
     *
     * @ORM\ManyToMany(targetEntity="Role",inversedBy="roles")
     * @ORM\JoinTable(name="hw_users_roles",
     *      joinColumns={@ORM\JoinColumn(name="user_id",referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\JoinColumn(name="role_id",referencedColumnName="id")})
     */
    private $roles;
    
    public function __construct(){
        parent::__construct();
        $this->roles = new ArrayCollection();
    }

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
        return $this;
    }

    public function getFirstName()
    {
        return $this->firstName;
    }

    public function setFirstName($firstName)
    {
        $this->firstName = $firstName;
        return $this;
    }

    public function getLastName()
    {
        return $this->lastName;
    }

    public function setLastName($lastName)
    {
        $this->lastName = $lastName;
        return $this;
    }

    public function getPosition()
    {
        return $this->position;
    }

    public function setPosition(string $position)
    {
        $this->position = $position;
        return $this;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail(string $email)
    {
        $this->email = $email;
        return $this;
    }

    public function getPhone()
    {
        return $this->phone;
    }

    public function setPhone(string $phone)
    {
        $this->phone = $phone;
        return $this;
    }

    public function getMobilePhone()
    {
        return $this->mobilePhone;
    }

    public function setMobilePhone(string $mobilePhone)
    {
        $this->mobilePhone = $mobilePhone;
        return $this;
    }

    //get role's title array
    public function getRoles()
    {
        $roleTitleArray = array();
        foreach($this->roles as $tempRole){
            array_push($roleTitleArray,$tempRole->getTitle());
        }
        return $roleTitleArray;
    }

    public function setRoles(ArrayCollection $roles)
    {
        $this->roles = $roles;
        return $this;
    }
    
    public function addRole(Role $role){
        $this->roles->add($role);
        return $this;
    }
 
    public function hasAdminRole(){
        if(in_array($this->getRoles(),'admin')){
            return true;
        }
        return false;
    }
    
    
}
